DEFINITION FOR C MODULE StdLib ;

FROM SYSTEM IMPORT ADDRESS, STRING, BYTE ;

TYPE
  STRINGPtr = POINTER TO STRING ;

CONST
  EXIT_FAILURE	= 1 ;
  EXIT_SUCCESS	= 0 ;

  ERANGE	= 2 ;

  RAND_MAX	= MAX( LONGINT ) ;

  LONG_MAX	= 07FFFFFFFH ;
  LONG_MIN	= 080000000H ;

  HUGE_VAL	= 1.0E300 ;

VAR
  errno : LONGINT ; (* duplicated from errno.def *)
    (* set this to zero before calling a function and test it afterwards *)
    (* eg. errno := 0 ;							 *)
    (*	   x := atof( str ) ;				 		 *)
    (*     IF errno # 0 THEN (* handle error *) END 			 *)

PROCEDURE abort( ) ;
(* aborts the program, as exit(20) *)

PROCEDURE abs( int : LONGINT ) : LONGINT ;
PROCEDURE labs( long : LONGINT ) : LONGINT ;

PROCEDURE exit( rc : LONGINT ) ;
(* exits the program with return code rc *)

PROCEDURE atexit( p : PROC ) : LONGINT ;
(* At program exit p will be called.					*)
(* This is normally a procedure that frees any aquired resources etc	*)
(* atexit returns non-zero if p could not be installed.			*)

PROCEDURE atoi( cs : STRING ) : LONGINT  ;
(* converts string cs to longint , as strtol( cs , NIL , 10 ) *)

PROCEDURE atol( cs : STRING ) : LONGINT  ;
(* as atoi *)

PROCEDURE atof( cs : STRING ) : LONGREAL ;
(* converts cs to a longreal , same as strtod( cs , NIL ) *)

PROCEDURE strtod( cs : STRING ; endp : STRINGPtr ) : LONGREAL ;
(* converts cs to a longreal , leading spaces are ignored		*)
(* If the conversion will overflow then (+/-)HUGE_VAL is returned.	*)
(* Elsif the result will underflow then 0 is returned.			*)
(* In both cases errno is set to ERANGE.				*)
(* If endp # NIL then endp^ points to any unconverted suffix of cs	*)

PROCEDURE strtol( cs : STRING ; endp : STRINGPtr ; base : LONGINT ) : LONGINT ;
(* converts cs to a longint , leading spaces are ignored.		   *)
(* Base can be 0 or a number between 2 & 36.				   *)
(* If base = 0 then it is assumed 8 if cs start with a 0 , 16 if cs starts *)
(*  0x or 0X , base 10 otherwise.					   *)
(* If base = 16 the cs can start with 0x or 0X				   *)
(* If the conversion will overflow then LONG_MAX or LONG_MIN is returned   *)
(* In both cases errno is set to ERANGE.				   *)
(* If endp # NIL then endp^ points to any unconverted suffix of cs	   *)

PROCEDURE getenv( ev : STRING ) : STRING ;
(* getenv returns the environment variable associated with ev		*)
(* (environment variables are set using the amigaDOS SETENV command)	*)
(* or NIL if ev could not be found.					*)

PROCEDURE malloc( size : LONGINT ) : ADDRESS ;
(* Allocates and returns size bytes of uninitialized heap storage *)
(* malloc returns NIL if the memory could not be found.		  *)

PROCEDURE calloc( nobj : LONGINT ; size : LONGINT ) : ADDRESS ;
(* Allocates and returns an array of nobj elements whose	*)
(* element_size = size bytes. The memory is initialized to 0's.	*)
(* calloc returns NIL if the memory could not be found.		*)

PROCEDURE realloc( adr : ADDRESS ; size : LONGINT ) : ADDRESS ;
(* reallocates memory from a previous call to malloc, calloc or realloc	     *)
(* The old contents of the old area is copied to the new area,		     *)
(* if size is smaller than the previous size then only size bytes are copied *)
(* otherwise the extra area is unitialized. realloc returns NIL if it fails  *)
(* to reallocate the memory.						     *)

PROCEDURE free( adr : ADDRESS ) ;
(* frees memory allocated from one of above 3 functions. adr may be NIL *)

PROCEDURE rand( ) : LONGINT ;
(* returns a psuedo random number in the range 0 to RAND_MAX *)

PROCEDURE srand( seed : LONGINT ) ;
(* reseeds rand.The initial seed is 1 *)

PROCEDURE system( cs : STRING ) : LONGINT ;
(* passes cs to amigaDOS for execution, the returned value may or may not *)
(* be the return value of the command (depending on which version of      *)
(* amigaDOS is in use ,V36 should return something useful)		  *)

TYPE
  CompProc = PROCEDURE( ADDRESS, ADDRESS ) : LONGINT ;
(* bsearch & qsort expect a procedure parameter that returns a -ve result *)
(* if the first arg is greater than the second , 0 is both are same ,     *)
(*  a +ve result otherwise.						  *)

PROCEDURE bsearch( VAR key   : ARRAY OF BYTE ;
		   VAR base  : ARRAY OF BYTE ;
		   num, size : LONGINT ;
		   cmpProc   : CompProc ) : ADDRESS ;

(* Does a binary chop lookup of key in base, num = number of elements in base *)
(* size = size of key & size of each element in base.			      *)
(* Elements in base must be in ascending order.			              *)
(* bsearch returns NIL if the key could not be matched otherwise a pointer to *)
(* to the matching element.						      *)
(* The address of key is passed as the first parameter to cmpProc.	      *)

PROCEDURE qsort( VAR base    : ARRAY OF BYTE ;
		     n, size : LONGINT ;
		     cmp     : CompProc ) ;
(* Sorts base into ascending order *)

(* Non-ANSI functions *)

TYPE
  IntProc = PROCEDURE( ) : LONGINT ;

PROCEDURE onbreak( handler : IntProc ) : IntProc ;
(* handler is called if chkabort detects a break signal, returns the	      *)
(* old break handler.							      *)
(* If the handler returns 0 then the program continues execution otherwise    *)
(* the program exits.The default DICE handler terminates program execution.   *)

PROCEDURE chkabort( ) ;
(* Checks to see if the program has been sent a break signal eg ^C pressed.   *)
(* If a break is detected then the the break handler is called.		      *)
(* Note some library functions (in StdIO) call chkabort and should therefore  *)
(* not be called in critical sections of your program unless youve disabled   *)
(* break checking by installing your own break handler.			      *)

PROCEDURE expand_args(     argcIn  : LONGINT ;
                           argvIn  : ADDRESS ;
                       VAR argvOut : LONGINT ;
                       VAR argcOut : ADDRESS ) : LONGINT ;

(* This function will expand any amigaDOS wildcards that were specified on    *)
(* the command line, A typical call might be:				      *)
(*  err := StdLib.expand_args( M2Lib.argc, M2Lib.argc, M2Lib.argc, M2Lib.argv)*)
(* Return 0 on success, non-zero otherwise (run out of stack etc)             *)
(* Any program that uses expand_args() should be run with at least 8K of stack*)

END StdLib.

